#include "context.h"
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

Context::Context()
{
	cout << "创建环境实例..." << endl;
	this->state1_ = new ConcreteState1(this);
	this->state2_ = new ConcreteState2(this);
	this->current_state_ = this->state1_;
}

Context::~Context()
{
	if (NULL != this->state1_)
	{
		delete this->state1_;
		this->state1_ = NULL;
	}

	if (NULL != this->state2_)
	{
		delete this->state2_;
		this->state2_ = NULL;
	}
	cout << "释放环境实例..." << endl;
}

void Context::SetState(State* state)
{
	if (NULL == state)
	{
		cout << "failed to set state: state is NULL!" << endl;
		return;
	}
	
	this->current_state_ = state;
}

State* Context::GetState1()
{
	return this->state1_;
}

State* Context::GetState2()
{
	return this->state2_;
}

void Context::handle1()
{
	if (NULL == this->current_state_)
	{
		cout << "failed to run handle1: current state is NULL!" << endl;
		return;
	}
	this->current_state_->handle1();
}

void Context::handle2()
{
	if (NULL == this->current_state_)
	{
		cout << "failed to run handle2: current state is NULL!" << endl;
		return;
	}
	this->current_state_->handle2();
}
